package com.wiwf.model.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wiwf.model.manage.bean.TreeMenuBean;
import com.wiwf.model.manage.service.PageIndexService;
import com.wiwf.model.mapper.admin.bean.SysMenuEntity;
import com.wiwf.model.security.service.SysTokenService;

/**
 * @author kehui
 */
@Service
public class PageIndexServiceImpl implements PageIndexService{

	@Autowired
	private SysTokenService sysTokenService;
	
	@Override
	public List<TreeMenuBean> getNodeTreeMenus(String token){
		
		return getNodeTreeMenus(sysTokenService.getTokenUser(token).getMenus());
	}
	
	@Override
	public List<TreeMenuBean> getNodeTreeMenus(List<SysMenuEntity> baseMenus){
		
		List<TreeMenuBean> resList = new ArrayList<TreeMenuBean>();
		for (SysMenuEntity menu : baseMenus) {
			if(menu.getParentId() != 0) {
				continue;
			} else {
				TreeMenuBean treeMenu = getTreeMenuBean(baseMenus, menu);
				
				resList.add(treeMenu);
			}
		}
		return resList;
	}

	@Override
	public void getNodeTreeMenus(List<SysMenuEntity> baseMenus, TreeMenuBean ttMenu){
		for (SysMenuEntity menu : baseMenus) {
			if(ttMenu.getMenuId().equals(menu.getParentId())) {
				TreeMenuBean treeMenu = getTreeMenuBean(baseMenus, menu);
				
				ttMenu.getChildren().add(treeMenu);
			}
		}
	}
	
	private TreeMenuBean getTreeMenuBean(List<SysMenuEntity> baseMenus, SysMenuEntity menu) {
		TreeMenuBean treeMenu = new TreeMenuBean();
		treeMenu.setMenuId(menu.getMenuId());treeMenu.setParentId(menu.getParentId());
		treeMenu.setId(menu.getPath());
		treeMenu.setIcon(menu.getIcon());
		treeMenu.setTitle(menu.getMenuName());
		treeMenu.setType("");

		if(menu.getType() == 0){
			treeMenu.setChildren(new ArrayList<TreeMenuBean>());
			getNodeTreeMenus(baseMenus, treeMenu);
		} else if(menu.getType() == 1){
			treeMenu.setType("open");
		} else if(menu.getType() == 2){
			treeMenu.setType("modal");
		} else if(menu.getType() == 3){
			treeMenu.setType("blank");
		}
		return treeMenu;
	}
	
}
